﻿Option Strict On

Imports System.Collections
Imports System.Windows.Forms
Imports log4net
Imports log4net.Appender
Imports log4net.Repository.Hierarchy
Imports log4net.Core

Namespace log4net.Appender

    Public Class TextBoxAppender
        Inherits AppenderSkeleton

        Private _textBox As System.Windows.Forms.TextBox

        Public Sub New()
        End Sub

        Protected Overrides Sub Append(ByVal LoggingEvent As LoggingEvent)
            If Not _textBox.IsDisposed Then _
                _textBox.AppendText(RenderLoggingEvent(LoggingEvent))
        End Sub

        Public Property TextBox() As TextBox
            Get
                Return _textBox
            End Get
            Set(ByVal value As TextBox)
                _textBox = value
            End Set
        End Property

        Public Shared Sub SetTextBox(ByVal tb As TextBox)
            For Each appender As IAppender In GetAppenders()
                If TypeOf appender Is TextBoxAppender Then
                    DirectCast(appender, TextBoxAppender).TextBox = tb
                End If
            Next
        End Sub

        Private Shared Function GetAppenders() As IAppender()
            Dim appenders As New ArrayList()

            appenders.AddRange(LogManager.GetRepository().GetAppenders())
            For Each log As ILog In LogManager.GetCurrentLoggers()
                appenders.AddRange(DirectCast(log.Logger, Logger).Appenders)
            Next

            Return DirectCast(appenders.ToArray(GetType(IAppender)), IAppender())
        End Function
    End Class

End Namespace